<template>
	<view class="container">
		<view class="header">
			<text class="title">请选择您的性别</text>
			<text class="subtitle">选择性别后，我们将为您推荐更合适的匹配</text>
		</view>
		
		<view class="sex-select">
			<view 
				class="sex-option male" 
				:class="{selected: selectedSex === '男'}"
				@click="selectSex('男')"
			>
				<image 
					class="sex-icon" 
					:src="selectedSex === '男' ? 'http://xlnc.oss-cn-shenzhen.aliyuncs.com/uploads/icons8-%E7%81%AB%E6%98%9F%E7%AC%A6%E5%8F%B7-94.png' : 'http://xlnc.oss-cn-shenzhen.aliyuncs.com/uploads/icons8-%E7%81%AB%E6%98%9F%E7%AC%A6%E5%8F%B7-94.png'" 
				/>
				<text class="sex-text">我是男生</text>
			</view>
			
			<view 
				class="sex-option female" 
				:class="{selected: selectedSex === '女'}"
				@click="selectSex('女')"
			>
				<image 
					class="sex-icon" 
					:src="selectedSex === '女' ? 'http://xlnc.oss-cn-shenzhen.aliyuncs.com/uploads/icons8-%E5%A5%B3-80.png' : 'http://xlnc.oss-cn-shenzhen.aliyuncs.com/uploads/icons8-%E5%A5%B3-80.png'" 
				/>
				<text class="sex-text">我是女生</text>
			</view>
		</view>
		
		<button 
			class="confirm-btn" 
			:disabled="!selectedSex" 
			@click="confirmSex"
		>
			确认
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedSex: '',
				id: ''
			}
		},
		onLoad() {
			this.id = uni.getStorageSync('my_info').id;
		},
		methods: {
			selectSex(sex) {
				this.selectedSex = sex;
			},
			async confirmSex() {
				if (!this.selectedSex) {
					uni.showToast({
						title: '请选择性别',
						icon: 'none'
					});
					return;
				}
				
				uni.showLoading({
					title: '保存中...'
				});
				
				try {
					const res = await this.$myRuquest({
						url: '/index/dsnv.activities/updateSex',
						method: 'POST',
						data: {
							id: this.id,
							sex: this.selectedSex
						}
					});
					
					if (res.data.code === 200) {
						uni.hideLoading();
						uni.showToast({
							title: '性别设置成功',
							icon: 'success'
						});
						
						// 更新本地存储的性别信息
						let userInfo = uni.getStorageSync('my_info');
						userInfo.sex = this.selectedSex;
						uni.setStorageSync('my_info', userInfo);
						
						// 返回上一页
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					} else {
						uni.hideLoading();
						uni.showToast({
							title: res.data.msg || '性别设置失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx;
		height: 100vh;
		background-color: #f8f8f8;
	}
	
	.header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 80rpx;
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}
	
	.subtitle {
		font-size: 28rpx;
		color: #999;
	}
	
	.sex-select {
		display: flex;
		justify-content: space-around;
		width: 100%;
		margin-bottom: 80rpx;
	}
	
	.sex-option {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 300rpx;
		height: 400rpx;
		border-radius: 20rpx;
		background-color: #fff;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		transition: all 0.3s;
	}
	
	.sex-option.selected {
	}
	
	.male.selected {
		border: 4rpx solid #435ff1;
		box-shadow: 0 4rpx 20rpx rgba(19, 126, 221,0.3);
		background-color: rgba(64, 158, 255, 0.05);
	}
	
	.female.selected {
		border: 4rpx solid #F14399;
		box-shadow: 0 4rpx 20rpx rgba(241, 67, 153, 0.3);
		background-color: rgba(255, 64, 158, 0.05);
	}
	
	.sex-icon {
		width: 160rpx;
		height: 160rpx;
		margin-bottom: 40rpx;
	}
	
	.sex-text {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
	}
	
	.confirm-btn {
		width: 80%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #F14399;
		color: #fff;
		font-size: 32rpx;
		border-radius: 45rpx;
		margin-top: 60rpx;
	}
	
	.confirm-btn[disabled] {
		background-color: #ccc;
	}
</style>